package turtle.system.doug;
import java.util.*;

import turtle.system.pt.StatCalc;

public class StatCalcSet {
	private TreeMap<Integer, StatCalc> statMap = new TreeMap<Integer, StatCalc>();
	public static final int TURTLE_TOTAL_STAT = 1;
	public static final int TURTLE_TOTAL_ADDSIGNAL_STAT = 2;	
	public static final int TURTLE_TOTAL_TRADE_IN_SPECIAL_HANDLE_STAT = 3;
	public static final int TURTLE_TOTAL_LONG_STAT = 4;
	public static final int TURTLE_TOTAL_SHORT_STAT = 5;
	public static final int TURTLE_TOTAL_ADDSIGNAL_LONG_STAT = 6;	
	public static final int TURTLE_TOTAL_ADDSIGNAL_SHORT_STAT = 7;		
	public static final int TURTLE_NON_HOLD_TRADE_STAT = 8;	
	public static final int TURTLE_INDAY_DIFF_TRADE_STAT = 9;	
	
	public static final int STOP_LOSS_ALL_OUT = 10;
	public static final int STOP_LOSS_LONG_OUT = 11;
	public static final int STOP_LOSS_SHORT_OUT = 12;
	
	public static final int STOP_PROFIT_ALL_OUT = 21;
	public static final int STOP_PROFIT_LONG_OUT = 22;
	public static final int STOP_PROFIT_SHORT_OUT = 23;
	
	public static final int TREND_REVERSE_ALL_OUT = 31;
	public static final int TREND_REVERSE_LONG_OUT = 32;
	public static final int TREND_REVERSE_SHORT_OUT = 33;

	public static final int FORCE_LOSS_ALL_OUT = 41;
	public static final int FORCE_LOSS_LONG_OUT = 42;
	public static final int FORCE_LOSS_SHORT_OUT = 43;	

	public static final int KEEP_TRADE_LONG = 51;
	public static final int KEEP_TRADE_SHORT = 52;
	public static final int ADVANCED_TRADE_IN_LONG = 53;
	public static final int ADVANCED_TRADE_IN_SHORT = 54;

	public static final int OUT_SIGNAL_ALL_InDay	 = 101;
	public static final int OUT_SIGNAL_LONG_InDay	 = 102; 
	public static final int OUT_SIGNAL_SHORT_InDay	 = 103;

	public static final int OUT_SIGNAL_STOP_LOSS_LONG_InDay	 = 111;
	public static final int OUT_SIGNAL_STOP_LOSS_SHORT_InDay	 = 112;
	public static final int OUT_SIGNAL_STOP_PROFIT_LONG_InDay	 = 113;
	public static final int OUT_SIGNAL_STOP_PROFIT_SHORT_InDay	 = 114;
	public static final int OUT_SIGNAL_TREND_REVERSE_LONG_InDay	 = 115;
	public static final int OUT_SIGNAL_TREND_REVERSE_SHORT_InDay	 = 116;

	public static final int IN_SIGNAL_ALL_InDay	= 121;
	public static final int IN_SIGNAL_LONG_InDay	= 122;
	public static final int IN_SIGNAL_SHORT_InDay = 123;	
	public static final int IN_SIGNAL_SECOND_DAY_LONG_InDay = 124;
	public static final int IN_SIGNAL_SECOND_DAY_SHORT_InDay = 125;	
	
	public static final int FILTER_SIGNAL_ALL = 131;
	public static final int FILTER_SIGNAL_CONTINUES_PROFIT = 132;
	
	public static final int SIMULATION_PROFIT = 232;
    public StatCalcSet() {
    	
		statMap.put(TURTLE_TOTAL_STAT, new StatCalc("(基本單)總資訊"));
		statMap.put(TURTLE_TOTAL_TRADE_IN_SPECIAL_HANDLE_STAT, new StatCalc("(基本單)總續留or提前入場資訊"));		
		statMap.put(TURTLE_TOTAL_LONG_STAT, new StatCalc("(基本單)總多單資訊"));
		statMap.put(TURTLE_TOTAL_SHORT_STAT, new StatCalc("(基本單)總空單資訊"));
		statMap.put(TURTLE_TOTAL_ADDSIGNAL_STAT, new StatCalc("(加碼單)總加碼系統資訊"));
		statMap.put(TURTLE_TOTAL_ADDSIGNAL_LONG_STAT, new StatCalc("(加碼單)總加碼系統多單資訊"));
		statMap.put(TURTLE_TOTAL_ADDSIGNAL_SHORT_STAT, new StatCalc("(加碼單)總加碼系統空單資訊"));
		statMap.put(TURTLE_NON_HOLD_TRADE_STAT, new StatCalc("(加碼單)雙向突破不進場日空單進場資訊"));
		statMap.put(TURTLE_INDAY_DIFF_TRADE_STAT, new StatCalc("(加碼單)正逆價差進場資訊"));
		
		statMap.put(STOP_LOSS_ALL_OUT, new StatCalc("(基本單)整體停損出場"));
		statMap.put(STOP_LOSS_LONG_OUT, new StatCalc("(基本單)多單停損出場"));
		statMap.put(STOP_LOSS_SHORT_OUT, new StatCalc("(基本單)空單停損出場"));

		statMap.put(STOP_PROFIT_ALL_OUT, new StatCalc("(基本單)整體停利出場"));
		statMap.put(STOP_PROFIT_LONG_OUT, new StatCalc("(基本單)多單停利出場"));
		statMap.put(STOP_PROFIT_SHORT_OUT, new StatCalc("(基本單)空單停利出場"));

		statMap.put(TREND_REVERSE_ALL_OUT, new StatCalc("(基本單)整體趨勢反轉出場"));
		statMap.put(TREND_REVERSE_LONG_OUT, new StatCalc("(基本單)多單趨勢反轉出場"));
		statMap.put(TREND_REVERSE_SHORT_OUT, new StatCalc("(基本單)空單趨勢反轉出場"));
		
		statMap.put(FORCE_LOSS_ALL_OUT, new StatCalc("(基本單)總強制盤中停損出場"));
		statMap.put(FORCE_LOSS_LONG_OUT, new StatCalc("(基本單)多單強制盤中停損出場"));
		statMap.put(FORCE_LOSS_SHORT_OUT, new StatCalc("(基本單)空單強制盤中停損出場"));		
		
		statMap.put(KEEP_TRADE_LONG, new StatCalc("(基本單)多單昨單續留"));
		statMap.put(KEEP_TRADE_SHORT, new StatCalc("(基本單)空單昨單續留"));
		statMap.put(ADVANCED_TRADE_IN_LONG, new StatCalc("(基本單)多單提前入場"));
		statMap.put(ADVANCED_TRADE_IN_SHORT, new StatCalc("(基本單)空單提前入場"));
		
		statMap.put(OUT_SIGNAL_ALL_InDay, new StatCalc("(加碼單)出場訊號總體加碼"));
		statMap.put(OUT_SIGNAL_LONG_InDay, new StatCalc("(加碼單)出場訊號作多加碼"));
		statMap.put(OUT_SIGNAL_SHORT_InDay, new StatCalc("(加碼單)出場訊號作空加碼"));		

		statMap.put(OUT_SIGNAL_STOP_LOSS_LONG_InDay, new StatCalc("(加碼單)多單停損出場訊號加碼(今收盤入明收盤出)"));
		statMap.put(OUT_SIGNAL_STOP_LOSS_SHORT_InDay, new StatCalc("(加碼單)空單停損出場訊號加碼(今收盤入明收盤出)"));
		statMap.put(OUT_SIGNAL_STOP_PROFIT_LONG_InDay, new StatCalc("(加碼單)多單停利出場訊號加碼(今收盤入明開盤出:未定,可以finetune)"));
		statMap.put(OUT_SIGNAL_STOP_PROFIT_SHORT_InDay, new StatCalc("(加碼單)空單停利出場訊號加碼(明開盤入明收盤出)"));
		statMap.put(OUT_SIGNAL_TREND_REVERSE_LONG_InDay, new StatCalc("(加碼單)多單趨勢反轉出場訊號加碼(明開盤入明收盤出)"));
		statMap.put(OUT_SIGNAL_TREND_REVERSE_SHORT_InDay, new StatCalc("(加碼單)空單趨勢反轉出場訊號加碼(明開盤入明收盤出)"));

		statMap.put(IN_SIGNAL_ALL_InDay, new StatCalc("(加碼單)進場訊號總體加碼"));
		statMap.put(IN_SIGNAL_LONG_InDay, new StatCalc("(加碼單)進場訊號作多加碼"));
		statMap.put(IN_SIGNAL_SHORT_InDay, new StatCalc("(加碼單)進場訊號作空加碼"));		
		statMap.put(IN_SIGNAL_SECOND_DAY_LONG_InDay, new StatCalc("(加碼單)進場訊號作多快速作多"));		
		statMap.put(IN_SIGNAL_SECOND_DAY_SHORT_InDay, new StatCalc("(加碼單)進場訊號作空快速作空"));		
		
		statMap.put(FILTER_SIGNAL_ALL, new StatCalc("(過濾單)全部過濾單"));
		statMap.put(FILTER_SIGNAL_CONTINUES_PROFIT, new StatCalc("(過濾單)連續獲利過濾"));
		
		statMap.put(SIMULATION_PROFIT, new StatCalc("實驗中資訊"));
		
    }
    public double getLeastContinusProfit(int statType) {
    	double ret = 0;
    	StatCalc calc = statMap.get(statType);
    	ArrayList<Double> hist = calc.valueList;
    	for (int i=hist.size()-1;i>=0;i--) {
    		if (hist.get(i) <0) {
    			break;
    		}
    		else {
    			ret += hist.get(i);
    		}
    	}
    	return ret;
    }
    public void knownContinueFailProb(int statType, int num) {
    	int contCount = 0;    	
    	int satCount = 0;
    	int confidentCount = 0;
    	StatCalc calc = statMap.get(statType);
    	
    	ArrayList<Double> hist = calc.valueList;
    	for (int i=0;i<hist.size();i++) {
    		if (hist.get(i) <=0) {
    			contCount++;
    		}
    		else {
    			contCount = 0;
    		}
    		if (contCount>=num) {
    			satCount++;
    		}
    		if (contCount>=num-1) {
    			confidentCount++;
    		}
    	}
    	System.out.println("連賠"+num+"次的機率="+satCount+"/"+(hist.size()-num)+"="+((double)satCount/((double)(hist.size()-num))));
    	System.out.println("(連賠"+(num-1)+"次的次數="+confidentCount+","+"而連賠"+num+"次的次數為"+satCount+
    			",confidence = "+ (double)satCount/(double)confidentCount+")");
    			
    }
    public void knownContinueSuccessProb(int statType, int num) {
    	int contCount = 0;    	
    	int satCount = 0;
    	StatCalc calc = statMap.get(statType);
    	
    	ArrayList<Double> hist = calc.valueList;
    	for (int i=0;i<hist.size();i++) {
    		if (hist.get(i) >=0) {
    			contCount++;
    		}
    		else {
    			contCount = 0;
    		}
    		if (contCount>=num) {
    			satCount++;
    		}
    	}
    	System.out.println("連贏"+num+"次的機率="+satCount+"/"+(hist.size()-num)+"="+((double)satCount/((double)(hist.size()-num))));
    	
    }    
    
    public StatCalc testCalc = new StatCalc();
    public void enter(int statType, double average_outprice, double average_inprice, int trade_count, boolean longTrade) {
    	StatCalc stat = statMap.get(statType);
   		if (longTrade)
    	  stat.enter(((average_outprice-average_inprice)*trade_count)/4);
   		else 
   		  stat.enter(-((average_outprice-average_inprice)*trade_count)/4);	
   		
   		
   		if (longTrade && trade_count==2) {
   			testCalc.enter(((average_outprice-average_inprice)*trade_count)/4);
   		}
   		
   		if (!longTrade && trade_count==2) {
   			//testCalc.enter(-((average_outprice-average_inprice)*trade_count)/4);	
   		}
    }

    public void enter(int statType, double value) {
    	StatCalc stat = statMap.get(statType);
    	if (statType == TURTLE_TOTAL_STAT && PriceDataRefineSellSystem.useReverse == true){
    		stat.enter(-value);
    	}
    	else {
    		stat.enter(value);	
    	}
    	
    }
    public StatCalc getStat(int statType) {
    	return statMap.get(statType);
    }
    public String toString() {
    	StringBuffer buf = new StringBuffer();
    	Object[] keys = statMap.keySet().toArray();
    	for (int i=0;i<keys.length;i++) {
    	  buf.append(statMap.get(keys[i]).toString());
    	  buf.append("\n");
    	}
    	
    	return buf.toString();
    }
}
